<!-- 编辑弹窗 -->
<template>
    <ele-modal
      width="520px"
      :visible="visible"
      :close-on-click-modal="true"
      custom-class="ele-dialog-form"
      :title="isUpdate ? '修改账户' : '添加账户'"
      @update:visible="updateVisible"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="92px">
        <el-row :gutter="24">
          <el-col :md="24" :sm="24">
            <el-form-item label="账户:" prop="account">
               <el-input v-model="form.account"  placeholder="请输入账户" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" v-show="isShowPassword">
          <el-col :md="24" :sm="24" >
            <el-form-item label="初始密码:" >
              <el-input  v-model="form.password" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :md="24" :sm="24">
            <el-form-item label="姓名:"  prop="realName">
              <el-input v-model="form.realName" placeholder="请输入姓名" >
                  <template slot="append">
                    <el-select v-model="form.sex"  placeholder="性别" style="width: 80px; " class="ele-block" clearable>
                      <el-option v-for="(item, index) in sexList" :key="index" :label="item.Value" :value="item.Key" />
                    </el-select></template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :md="24" :sm="24" >
            <el-form-item label="手机号码:" >
              <el-input  v-model="form.phone" placeholder="请输入手机号码" />
            </el-form-item>
          </el-col>
        </el-row>
       
        <el-row :gutter="24">
          <el-col :md="24" :sm="24" >
            <el-form-item label="邮箱:" >
              <el-input  v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="updateVisible(false)">取消</el-button>
        <el-button type="primary" :loading="loading" @click="save" v-if="this.btnindex!='3'">
          保存
        </el-button>
      </div>
    </ele-modal>
  </template>
  
  <script>
    import { QuerySexEnum } from '@/api/enummanger';
    import { AddUser,UpdateUser,GetModelById } from '@/api/usermanager/user';
    export default {
      name: 'UserEdit',
      props: {
        // 弹窗是否打开
        visible: Boolean,
        // 修改回显的数据
        data: Object,
        //按钮索引
        btnindex:String,
      },
      data() {
        return {
          // 表单数据
          form: {},
          // 表单验证规则
          rules: {
            account: [{ required: true, message: '请输入账户',trigger: 'blur'}],
            realName: [{required: true,message: '请输入姓名',trigger: 'blur'}],
            phone: [{ required: true, message: '请输入手机号码',trigger: 'blur'}],
          },
          // 提交状态
          loading: false,
          // 是否是修改
          isUpdate: false,
           // 是否是修改
           isShowPassword: true,
              //性别
          sexList:[]
        };
      },
  
      //初始化数据
      created(){
        this.querySexEnums();
      },
      methods: {
       //性别
      async querySexEnums(){
        let sysEnums = await QuerySexEnum();
          sysEnums.map(e=>{
            this.sexList.push({Key:e.Key,Value:e.Value});
          });
        },
        /* 保存编辑 */
        save() {
          //校验必填信息
          this.$refs['form'].validate((valid) => {
            if (!valid) {return false;}
            this.loading = true;
            const saveOrUpdate = this.isUpdate ? UpdateUser : AddUser;
            saveOrUpdate(this.form)
              .then((msg) => {
                this.loading = false;
                this.$message.success(msg);
                this.updateVisible(false);
                //回调父组件方法
                this.$emit('done');
              })
              .catch((e) => {
                this.loading = false;
                this.$message.error(e.message);
              });
          });
        },
        /* 更新visible */
        updateVisible(value) {
          //更新父组件中的visible的值
          this.$emit('update:visible', value);
        },
           //赋值到页面
       setUser(id){
          GetModelById(id)
              .then((res) => {
                this.form = res;
          });
        },
      
      },
      /* 监听visible属性变化 */
      watch: {
        visible(visible) {
          if (visible) {
            if (this.data && this.data.id) {
              //赋值this.form
              this.form = {};
              this.setUser(this.data.id);
              this.isUpdate = this.isUpdate = this.data.id !=null && this.data.id !=undefined;
              this.isShowPassword=false;
            } else {
              this.form={};
              this.isUpdate = false;
              this.isShowPassword=true;
              this.form.password = 888888;
            }
          } else {
            //移除校验结果并重置字段
            // this.$refs['form'].resetFields();
            //移除校验结果
            this.$refs['form'].clearValidate();
            this.form = {};
          }
        }
      }
    };
  </script>
  